/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.eurotrader.converters;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Usuário
 */
public class ConverteData implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String str) {
        if (str == null || str.equals("")) {
            return null;
        }

        //Date date = null;

        try {
            //DateFormat formatter = ;
            return (java.util.Date) new SimpleDateFormat("dd/MM/yyyy").parse(str);

        } catch (ParseException e) {
            try {
                throw e;
            } catch (ParseException ex) {
                Logger.getLogger(ConverteData.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object valor) {
        String retorno = "";

        if (valor != null) {
            //DateFormat formatter = ;

            retorno = new SimpleDateFormat("dd/MM/yyyy").format(valor);
        }
        return retorno;
    }
}
